import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from '@ohos/axios'

const request = axios.create({
  baseURL: "https://slwl-api.itheima.net/driver",
  timeout: 20000
})
request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  const token=AppStorage.get("token")
  if(token){
    config.headers.token=token
  }
  console.log(config.headers.token)
  return config
})
request.interceptors.response.use((response: AxiosResponse) => {
  if (response.data.code == 200) {
    return response.data.data
  } else {
return response.data
    return Promise.reject(response.data.msg)
  }
},
  (error: AxiosError) => {
    return Promise.reject(error.message)
  }
)

export default class Http {
  get<T>(url: string, params?: object) {
    return request.get<null, T>(url, {
      params
    })
  }

  post<T>(url: string, data?: object) {
    return request.post<null, T>(url, data)
  }

  put<T>(url: string, data?: object) {
    return request.put<null, T>(url, data)
  }

  delete<T>(url: string, params?: object) {
    return request.delete<null, T>(url, {
      params
    })
  }
}
export const http = new Http()